#include "../mqserver/Route.hpp"
#include <gtest/gtest.h>
#include <iostream>
#include <unordered_map>
using namespace std;

class RounterTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        cout << "测试环境初始化" << endl;
    }
    virtual void TearDown() override
    {
        cout << "测试环境清理" << endl;
    }
};
TEST(router, binding_key)
{
    string b1 = "#.#.aaa.bbb.123"; //
    string b2 = "aaa.*.123..bbb";
    string b3 = "aaa.*.#.bbb"; //
    string b4 = "aaa.#.bb_b";
    string b5 = "aaa..#.bbb";
    string b6 = "aaa.bbb..#";
    string b7 = "aaa.bbb..*";
    string b8 = "aaa.bbb..*12";
    string b9 = "aaa.bbb..#ab";
    ASSERT_EQ(mq::Router::isLegalBindingKey(b1), false);
    ASSERT_EQ(mq::Router::isLegalBindingKey(b2), true);
    ASSERT_EQ(mq::Router::isLegalBindingKey(b3), false);
    ASSERT_EQ(mq::Router::isLegalBindingKey(b4), true);
    ASSERT_EQ(mq::Router::isLegalBindingKey(b5), true);
    ASSERT_EQ(mq::Router::isLegalBindingKey(b6), true);
    ASSERT_EQ(mq::Router::isLegalBindingKey(b7), true);
    ASSERT_EQ(mq::Router::isLegalBindingKey(b8), false);
}
TEST(router, routing_key)
{
    string b1 = "#.#.aaa.bbb.123"; //
    string b2 = "aaa.*.123..bbb";  //
    string b3 = "aaa.*.#.bbb";     //
    string b4 = "aaa.bb_b";
    string b5 = "aaa..123.bbb";
    string b6 = "aaa.bbb.._21";
    string b7 = "aaa.bbb..3213_";
    ASSERT_EQ(mq::Router::isLegalRoutingKey(b1), false);
    ASSERT_EQ(mq::Router::isLegalRoutingKey(b2), false);
    ASSERT_EQ(mq::Router::isLegalRoutingKey(b3), false);
    ASSERT_EQ(mq::Router::isLegalRoutingKey(b4), true);
    ASSERT_EQ(mq::Router::isLegalRoutingKey(b5), true);
    ASSERT_EQ(mq::Router::isLegalRoutingKey(b6), true);
    ASSERT_EQ(mq::Router::isLegalRoutingKey(b7), true);
}
TEST(router, routering)
{
    std::vector<std::string> bkeys = {
        "aaa",
        "aaa.bbb",
        "aaa.bbb",
        "aaa.bbb",
        "aaa.bbb.ccc",
        "aaa.*",
        "aaa.*.bbb",
        "*.aaa.bbb",
        "#",
        "aaa.#",
        "aaa.#",
        "aaa.#.ccc",
        "aaa.#.ccc",
        "aaa.#.ccc",
        "#.ccc",
        "#.ccc",
        "aaa.#.ccc.ccc",
        "aaa.#.bbb.*.bbb"};
    std::vector<std::string> rkeys = {
        "aaa",
        "aaa.bbb",
        "aaa.bbb.ccc",
        "aaa.ccc",
        "aaa.bbb.ccc",
        "aaa.bbb",
        "aaa.bbb.ccc",
        "aaa.bbb",
        "aaa.bbb.ccc",
        "aaa.bbb",
        "aaa.bbb.ccc",
        "aaa.ccc",
        "aaa.bbb.ccc",
        "aaa.aaa.bbb.ccc",
        "ccc",
        "aaa.bbb.ccc",
        "aaa.bbb.ccc.ccc.ccc",
        "aaa.ddd.ccc.bbb.eee.bbb"};
    std::vector<bool> result = {
        true,
        true,
        false,
        false,
        true,
        true,
        false,
        false,
        true,
        true,
        true,
        true,
        true,
        true,
        true,
        true,
        true,
        true};
    for (int i = 0; i < bkeys.size(); i++)
    {
        ASSERT_EQ(mq::Router::route(mq::ExchangeType::TOPIC, rkeys[i], bkeys[i]), result[i]);
    }
}

int main(int argc, char *argv[])
{
    testing::AddGlobalTestEnvironment(new RounterTest);
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}